﻿
Imports System.Data
Imports EVOLTA.Promocion.Bl.Be
Imports EVOLTA.Promocion.Bl.Bc

Partial Class reportes_reporteResumen
    Inherits System.Web.UI.Page

#Region "Instancias a las Capas de Entidad y Negocio"

    Dim objnegProyecto As New EVOLTA.Promocion.Bl.Bc.Rproyecto
    Dim objnegSeparacionVenta As New EVOLTA.Promocion.Bl.Bc.RseparacionVenta

#End Region

#Region "Variables Globales"
    Dim dtbInformacion As New DataTable

    '  Declaración de variables para los totales
    Public dblTotalSoles1 As Double = 0
    Public dblTotalInafecto As Double = 0
    Public dblTotalAfecto As Double = 0
    Public dblTotalIgv As Double = 0
    Public dblTotalSoles2 As Double = 0

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not Page.IsPostBack) And (Request.Params("__EVENTTARGET") Is Nothing) Then
            Me.Cargar_años()
            Me.Cargar_meses()

            ' Reinicializando controles
            Me.lblSubTitulo2.ForeColor = Drawing.Color.Red
            Me.lblSubTitulo3.Text = String.Empty
            Me.lnkBtnExportar.Enabled = False
        Else
            If Me.ddlAnnio.SelectedItem.Text.ToString.Trim.Equals("-- SELECCIONE AÑO --") Or Me.ddlMes.SelectedItem.Value.ToString.Trim.Equals("0") Then
                Me.lblSubTitulo2.Text = String.Empty
                Me.lnkBtnExportar.Enabled = False
                Me.lblSubTitulo3.Text = "Para realizar una consulta debe de seleccionar un AÑO y MES."
                Me.lblSubTitulo3.ForeColor = Drawing.Color.Red
            Else
                Me.lnkBtnExportar.Enabled = True
                Me.lblSubTitulo2.ForeColor = Drawing.Color.Black
                Me.lblSubTitulo3.ForeColor = Drawing.Color.Black

                Me.lblSubTitulo2.Text = "Información correspondiente al año " & Me.ddlAnnio.SelectedItem.Text & " y mes " & Me.ddlMes.SelectedItem.Text

                Me.cargarInformacion()
            End If

            ViewState("vst_subtitulo_2") = Me.lblSubTitulo2.Text
        End If
    End Sub

    Private Sub Cargar_años()
        Dim dtbAnnios As New DataTable
        dtbAnnios = objnegSeparacionVenta.negCargar_años("EPAZCENPR")

        Me.ddlAnnio.DataSource = dtbAnnios
        Me.ddlAnnio.DataTextField = "Annio"
        Me.ddlAnnio.DataValueField = "Annio"
        Me.ddlAnnio.DataBind()
    End Sub

    Private Sub Cargar_meses()
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("-- SELECCIONE MES --", "0"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("ENERO", "1"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("FEBRERO", "2"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("MARZO", "3"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("ABRIL", "4"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("MAYO", "5"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("JUNIO", "6"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("JULIO", "7"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("AGOSTO", "8"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("SEPTIEMBRE", "9"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("OCTUBRE", "10"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("NOVIEMBRE", "11"))
        Me.ddlMes.Items.Add(New UI.WebControls.ListItem("DICIEMBRE", "12"))
    End Sub

    Private Sub cargarInformacion()
        Dim strProyectoId As String = Me.Master.idProyecto
        Dim strEtapaId As String = Me.Master.idEtapa
        Dim strAnnio As String = Me.ddlAnnio.SelectedItem.Text.ToString()
        Dim strMes As String = Me.ddlMes.SelectedItem.Value.ToString()

        Me.dtbInformacion = objnegSeparacionVenta.negCargar_reporte_resumen("EPAZCENPR", strProyectoId, strEtapaId, strAnnio, strMes)

        If Me.dtbInformacion.Rows.Count > 0 Then
            Me.rptReporteGlobal.DataSource = Me.dtbInformacion
            Me.rptReporteGlobal.DataBind()

            ' Líneas de código para hacer posible la exportación
            Dim dtbExportar As New DataTable
            dtbExportar.Columns.Add("DPTO")
            dtbExportar.Columns.Add("CLIENTE")
            dtbExportar.Columns.Add("ITEM")
            dtbExportar.Columns.Add("FAC")
            dtbExportar.Columns.Add("N/C")
            dtbExportar.Columns.Add("B/V")
            dtbExportar.Columns.Add("SEPARACION")
            dtbExportar.Columns.Add("CUOTA")
            dtbExportar.Columns.Add("S/")
            dtbExportar.Columns.Add("INAFECTA S/")
            dtbExportar.Columns.Add("AFECTA S/")
            dtbExportar.Columns.Add("IGV S/")
            dtbExportar.Columns.Add("TOTAL S/")
            dtbExportar.Columns.Add("OBS")

            Dim drwFila As DataRow

            For i As Integer = 0 To Me.dtbInformacion.Rows.Count - 1
                drwFila = dtbExportar.NewRow()
                drwFila("DPTO") = Me.dtbInformacion.Rows(i)("DPTO").ToString()
                drwFila("CLIENTE") = Me.dtbInformacion.Rows(i)("CLIENTE").ToString()
                drwFila("ITEM") = Me.dtbInformacion.Rows(i)("ITEM").ToString()
                drwFila("FAC") = Me.dtbInformacion.Rows(i)("FAC").ToString()
                drwFila("N/C") = Me.dtbInformacion.Rows(i)("N/C").ToString()
                drwFila("B/V") = Me.dtbInformacion.Rows(i)("B/V").ToString()
                drwFila("SEPARACION") = Format(CType(Me.dtbInformacion.Rows(i)("SEPARACION").ToString(), Double), "#,##0.00")
                drwFila("CUOTA") = Format(CType(Me.dtbInformacion.Rows(i)("CUOTA").ToString(), Double), "#,##0.00")
                drwFila("S/") = Format(CType(Me.dtbInformacion.Rows(i)("S/").ToString(), Double), "#,##0.00")
                drwFila("INAFECTA S/") = Format(CType(Me.dtbInformacion.Rows(i)("INAFECTA S/").ToString(), Double), "#,##0.00")
                drwFila("AFECTA S/") = Format(CType(Me.dtbInformacion.Rows(i)("AFECTA S/").ToString(), Double), "#,##0.00")
                drwFila("IGV S/") = Format(CType(Me.dtbInformacion.Rows(i)("IGV S/").ToString(), Double), "#,##0.00")
                drwFila("TOTAL S/") = Format(CType(Me.dtbInformacion.Rows(i)("TOTAL S/").ToString(), Double), "#,##0.00")
                drwFila("OBS") = Me.dtbInformacion.Rows(i)("OBS").ToString()
                dtbExportar.Rows.Add(drwFila)

                dblTotalSoles1 += CType(Me.dtbInformacion.Rows(i)("S/").ToString(), Double)
                dblTotalInafecto += CType(Me.dtbInformacion.Rows(i)("INAFECTA S/").ToString(), Double)
                dblTotalAfecto += CType(Me.dtbInformacion.Rows(i)("AFECTA S/").ToString(), Double)
                dblTotalIgv += CType(Me.dtbInformacion.Rows(i)("IGV S/").ToString(), Double)
                dblTotalSoles2 += CType(Me.dtbInformacion.Rows(i)("TOTAL S/").ToString(), Double)
            Next

            Dim dtbExportarTotal As New DataTable
            dtbExportarTotal.Columns.Add("Total General")
            dtbExportarTotal.Columns.Add("S/")
            dtbExportarTotal.Columns.Add("INAFECTA S/")
            dtbExportarTotal.Columns.Add("AFECTA S/")
            dtbExportarTotal.Columns.Add("IGV S/")
            dtbExportarTotal.Columns.Add("TOTAL S/")

            drwFila = dtbExportarTotal.NewRow()
            drwFila("Total General") = "TOTAL GENERAL"
            drwFila("S/") = Format(dblTotalSoles1, "#,##0.00")
            drwFila("INAFECTA S/") = Format(dblTotalInafecto, "#,##0.00")
            drwFila("AFECTA S/") = Format(dblTotalAfecto, "#,##0.00")
            drwFila("IGV S/") = Format(dblTotalIgv, "#,##0.00")
            drwFila("TOTAL S/") = Format(dblTotalSoles2, "#,##0.00")
            dtbExportarTotal.Rows.Add(drwFila)

            Session("ssn_Exportar") = dtbExportar
            Session("ssn_ExportarTotal") = dtbExportarTotal
        End If
    End Sub

    Protected Sub lnkBtnExportar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBtnExportar.Click
        Dim strNombreArchivo As String = "Reporte de Facturacion_" & Microsoft.VisualBasic.Right("0" & Date.Now.Day.ToString(), 2) & Microsoft.VisualBasic.Right("0" & Date.Now.Month.ToString(), 2) & Date.Now.Year.ToString() & "_" & Microsoft.VisualBasic.Right("0" & Date.Now.Hour.ToString(), 2) & Microsoft.VisualBasic.Right("0" & Date.Now.Minute.ToString(), 2) & Microsoft.VisualBasic.Right("0" & Date.Now.Second.ToString(), 2)
        Dim strTituloHoja As String = "REPORTE DE FACTURACIÓN- " & Me.Master.descProyecto.ToUpper.Trim()
        Dim strSubTituloHoja As String = ViewState("vst_subtitulo_2").ToString()

        Dim dtbInformacion As New DataTable
        Dim dtbTotales As New DataTable

        dtbInformacion = Session("ssn_Exportar")
        dtbTotales = Session("ssn_ExportarTotal")

        If dtbInformacion.Rows.Count > 0 Then
            Dim objInfraestructura As New Infraestructura

            objInfraestructura.Exportar_a_excel(strNombreArchivo, strTituloHoja, strSubTituloHoja, dtbInformacion, dtbTotales)
        End If
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.ModalPopupExtender.Hide()
    End Sub

End Class